package tc.de.krawallmann.gravoblocks.twl.widgets;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import tc.de.krawallmann.gravoblocks.game.model.BlockColor;
import tc.de.krawallmann.gravoblocks.game.model.TetrisBlock;
import de.matthiasmann.twl.AnimationState;
import de.matthiasmann.twl.BoxLayout;
import de.matthiasmann.twl.renderer.AnimationState.StateKey;

/**
 * The Class BlockView.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public class BlockView extends BoxLayout implements PropertyChangeListener {

	/** The Constant BLOCKED. */
	public static final StateKey BLOCKED = StateKey.get("blocked");

	/** The Constant BLUE. */
	public static final StateKey BLUE = StateKey.get("blue");

	/** The Constant CYAN. */
	public static final StateKey CYAN = StateKey.get("cyan");

	public static final StateKey GAMEOVER = StateKey.get("gameover");

	/** The Constant GHOST. */
	public static final StateKey GHOST = StateKey.get("ghost");

	/** The Constant GREEN. */
	public static final StateKey GREEN = StateKey.get("green");

	/** The Constant MARKEDTOREMOVE. */
	public static final StateKey MARKEDTOREMOVE = StateKey.get("markedToRemove");

	/** The Constant METALLIC. */
	public static final StateKey METALLIC = StateKey.get("metallic");

	/** The Constant ORANGE. */
	public static final StateKey ORANGE = StateKey.get("orange");

	/** The Constant PURPLE. */
	public static final StateKey PURPLE = StateKey.get("purple");

	/** The Constant RED. */
	public static final StateKey RED = StateKey.get("red");

	/** The Constant TRANSPARENT. */
	public static final StateKey TRANSPARENT = StateKey.get("transparent");

	/** The Constant WHITE. */
	public static final StateKey WHITE = StateKey.get("white");

	/** The Constant YELLOW. */
	public static final StateKey YELLOW = StateKey.get("yellow");

	/**
	 * Instantiates a new block view.
	 */
	public BlockView() {
		super();
		final AnimationState state = getAnimationState();
		state.setAnimationState(BlockView.RED, false);
		state.setAnimationState(BlockView.GAMEOVER, false);
		state.setAnimationState(BlockView.GREEN, false);
		state.setAnimationState(BlockView.BLUE, false);
		state.setAnimationState(BlockView.ORANGE, false);
		state.setAnimationState(BlockView.CYAN, false);
		state.setAnimationState(BlockView.YELLOW, false);
		state.setAnimationState(BlockView.GHOST, false);
		state.setAnimationState(BlockView.BLOCKED, false);
		state.setAnimationState(BlockView.TRANSPARENT, false);
		state.setAnimationState(BlockView.PURPLE, false);
		state.setAnimationState(BlockView.MARKEDTOREMOVE, false);
	}

	/**
	 * Instantiates a new block view.
	 * 
	 * @param block
	 *            the block
	 */
	public BlockView(final TetrisBlock block) {
		this();
		block.addPropertyChangeListener(this);
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.Widget#adjustSize()
	 */
	@Override
	public void adjustSize() {
		setSize(28, 28);
	}

	/*
	 * (non-Javadoc)
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
	 * PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(final PropertyChangeEvent event) {
		if ("color".equals(event.getPropertyName())) {
			setColor((BlockColor) event.getNewValue());
		} else if ("blocked".equals(event.getPropertyName())) {
			setBlocked((Boolean) event.getNewValue());
		} else if ("ghost".equals(event.getPropertyName())) {
			setGhost((Boolean) event.getNewValue());
		} else if ("transparent".equals(event.getPropertyName())) {
			setTransparent((Boolean) event.getNewValue());
		} else if ("markedToRemove".equals(event.getPropertyName())) {
			setMarkedToRemove((Boolean) event.getNewValue());
		} else if ("metallic".equals(event.getPropertyName())) {
			setMetallic((Boolean) event.getNewValue());
		}
	}

	/**
	 * Sets the blocked.
	 * 
	 * @param blocked
	 *            the new blocked
	 */
	public void setBlocked(final boolean blocked) {
		getAnimationState().setAnimationState(BlockView.BLOCKED, blocked);
	}

	/**
	 * Sets the color.
	 * 
	 * @param color
	 *            the new color
	 */
	public void setColor(final BlockColor color) {
		final AnimationState state = getAnimationState();
		state.setAnimationState(BlockView.RED, false);
		state.setAnimationState(BlockView.GREEN, false);
		state.setAnimationState(BlockView.BLUE, false);
		state.setAnimationState(BlockView.ORANGE, false);
		state.setAnimationState(BlockView.CYAN, false);
		state.setAnimationState(BlockView.YELLOW, false);
		state.setAnimationState(BlockView.WHITE, false);
		state.setAnimationState(BlockView.PURPLE, false);
		switch (color) {
			case RED: {
				state.setAnimationState(BlockView.RED, true);
				break;
			}
			case GREEN: {
				state.setAnimationState(BlockView.GREEN, true);
				break;
			}
			case PURPLE: {
				state.setAnimationState(BlockView.PURPLE, true);
				break;
			}
			case BLUE: {
				state.setAnimationState(BlockView.BLUE, true);
				break;
			}
			case CYAN: {
				state.setAnimationState(BlockView.CYAN, true);
				break;
			}
			case ORANGE: {
				state.setAnimationState(BlockView.ORANGE, true);
				break;
			}
			case YELLOW: {
				state.setAnimationState(BlockView.YELLOW, true);
				break;
			}
			default: {
				state.setAnimationState(BlockView.WHITE, true);
			}
		}
	}

	public void setGameover(final boolean gameover2) {
		getAnimationState().setAnimationState(BlockView.GHOST, gameover2);
	}

	/**
	 * Sets the ghost.
	 * 
	 * @param ghost
	 *            the new ghost
	 */
	public void setGhost(final boolean ghost) {
		getAnimationState().setAnimationState(BlockView.GHOST, ghost);
	}

	/**
	 * Sets the marked to remove.
	 * 
	 * @param markedToRemove
	 *            the new marked to remove
	 */
	public void setMarkedToRemove(final boolean markedToRemove) {
		getAnimationState().setAnimationState(BlockView.MARKEDTOREMOVE, markedToRemove);
	}

	public void setMetallic(final Boolean metallic) {
		getAnimationState().setAnimationState(BlockView.METALLIC, metallic);
	}

	/**
	 * Sets the transparent.
	 * 
	 * @param transparent
	 *            the new transparent
	 */
	public void setTransparent(final boolean transparent) {
		getAnimationState().setAnimationState(BlockView.TRANSPARENT, transparent);
	}

}
